<template>
  <div>
    <el-button type="primary" @click="dialogFormVisible = true">预约</el-button>
    <el-dialog title="预约详情" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="姓名👨" :label-width="formLabelWidth">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话📞" :label-width="formLabelWidth">
          <el-input v-model="form.tel" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUser(form)">确 定</el-button>
      </div>
    </el-dialog>
    <el-table :data="userList" style="width: 100%" :row-class-name="tableRowClassName">
      <el-table-column prop="id" label="编号" width="80"></el-table-column>
      <el-table-column prop="username" label="姓名" width="80"></el-table-column>
      <el-table-column prop="tel" label="电话"></el-table-column>
      <el-table-column prop="date" label="时间"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="warning"
            icon="el-icon-edit"
            @click="handleEdit(scope.$index, scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.$index, scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @current-change="getListInfo"
      @next-click="nextInfo"
      background
      layout="prev, pager, next"
      :total="totlelist"
    ></el-pagination>
  </div>
</template>

<script>
import dayjs from "dayjs";
export default {
  data() {
    return {
      userList: [],
      totlelist: null,
      dialogFormVisible: false,
      form: {
        username: "",
        tel: ""
      },
      formLabelWidth: "140px",
      page: null,
      br:{}
    };
  },
  components: {
  },
  watch: {
    dialogFormVisible: function(e) {
      if (!e) {
        this.form = {};
      }
    }
  },
  created() {
    this.getInfo(),
    this.$http.get("/api/userLength").then(res => {
      this.totlelist = res.data;
    })
    this.br=this.$route.meta;
    //console.log(this.$route.meta);
    
  },
  methods: {
    //格式化时间
    date(v) {
      return dayjs(v).format("MM/DD");
    },
    //编辑用户
    async editUser(r) {
      console.log(r._id);
      if (!r.id) {
        const res = await this.$http.put("/api/user", r);
        console.log(res.data);
        this.dialogFormVisible = false;
        this.getListInfo(this.page);
      }
      const res = await this.$http.put(`/api/user/${r._id}`, r);
      console.log(res.data);
      this.getListInfo();
      this.dialogFormVisible = false;
    },
    //获取当前页用户
    async getInfo(pageIndex = 1, pageSize = 10) {
      const res = await this.$http.get("/api/userPage", {
        params: {
          pageIndex,
          pageSize
        }
      });
      this.userList = res.data;
      this.userList.map(r => {
        if (r.updatedAt) {
          return (r.date = this.date(r.updatedAt));
        }
      });
    },
    getListInfo(pageIndex) {
      console.log(pageIndex);
      this.page = pageIndex;
      this.getInfo(pageIndex);
    },
    nextInfo(i) {
      console.log(i);
    },
    tableRowClassName({ rowIndex }) {
      if (rowIndex === 1) {
        return "warning-row";
      } else if (rowIndex === 3) {
        return "success-row";
      }
      return "";
    },
    handleEdit(index, row) {
      console.log(index, row);
      this.dialogFormVisible = true;
      this.form = row;
    },
    //删除这一行
    handleDelete(index, row) {
      console.log(index, row);
      this.$confirm(`此操作将永久删除${row.username}, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$http.delete(`/api/user/${row._id}`, row);
          //console.log(res.data);
          this.$message({
            type: "success",
            message: "删除成功!"
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>

<style lang="scss" scoped>
.user {
  flex: 1;
  padding: 10px;
}

.el-table .warning-row {
  background: oldlace;
}
.el-dialog {
  width: 39%;
}
.el-form-item {
  margin-right: 100px;
}
.el-table .success-row {
  background: #f0f9eb;
}
</style>